from itertools import product

from django_filters import FilterSet, rest_framework as filters
from rest_framework.decorators import permission_classes  # 导入权限装饰器
from rest_framework import viewsets, permissions
from rest_framework.filters import SearchFilter, OrderingFilter

from utils.pagination import MyPagination  # 导入自定义的分页类
from rest_framework.schemas import AutoSchema
from .models import PurchaseReceipt, PurchaseOrder, PurchaseReturn, PurchaseReceiptDetail, \
    PurchaseOrderDetail, PurchaseReturnDetail
from .serializers import PurchaseReceiptSerializer, PurchaseReceiptDetailSerializer, PurchaseOrderDetailSerializer, \
    PurchaseOrderSerializer, PurchaseReturnSerializer, PurchaseReturnDetailSerializer
from django_filters.rest_framework import DjangoFilterBackend  # 导入过滤后端


@permission_classes([permissions.IsAuthenticated])  # 设置权限为需要认证的用户
class BaseViewSet(viewsets.ModelViewSet):
    """
    基础视图集，用于减少重复代码。
    """
    # permission_classes = [permissions.IsAuthenticated]  # 设置权限为需要认证的用户
    # http_method_names = ['get', 'post', 'delete', 'put']  # 设置允许的HTTP方法
    # 可以添加一些通用的设置，例如权限控制、分页等
    pagination_class = MyPagination  # 设置分页类
    schema = AutoSchema()  # 设置自动生成文档
    filter_backends = [SearchFilter, OrderingFilter]  # 设置过滤后端


class PurchaseOrderViewSet(BaseViewSet):
    queryset = PurchaseOrder.objects.all()
    serializer_class = PurchaseOrderSerializer
    search_fields = ['vendor__name', 'purchase_order_code']  # 设置搜索字段
    ordering_fields = ['id', 'purchase_order_code']  # 设置排序字段


class PurchaseOrderDetailFilter(filters.FilterSet):
    status = filters.CharFilter(field_name='status')
    orderStatus = filters.CharFilter(field_name='purchase_order_code__order_status', lookup_expr='icontains')
    vendor = filters.CharFilter(field_name='purchase_order_code__vendor__name', lookup_expr='icontains')
    product_name = filters.CharFilter(field_name='product__name',  lookup_expr='icontains')
    purchase_order = filters.CharFilter(field_name='purchase_order_code__purchase_order_code', lookup_expr='icontains')

    class Meta:
        model = PurchaseOrderDetail
        fields = ['status', 'orderStatus', 'vendor', 'product_name', 'purchase_order' ]  # 设置搜索字段

class PurchaseOrderDetailViewSet(BaseViewSet):
    queryset = PurchaseOrderDetail.objects.all()
    serializer_class = PurchaseOrderDetailSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_class = PurchaseOrderDetailFilter
    ordering_fields = ['id', 'purchase_order_code']  # 设置排序字段


# 以下是具体的视图集，继承自BaseViewSet
class PurchaseReceiptViewSet(BaseViewSet):
    queryset = PurchaseReceipt.objects.all()
    serializer_class = PurchaseReceiptSerializer
    search_fields = ['vendor__name', 'purchase_order__code', 'receipt_date']  # 设置搜索字段
    ordering_fields = ['id', 'purchase_receipt_code', 'order_date', 'status']  # 设置排序字段


class PurchaseReceiptFilter(FilterSet):
    purchase_order_detail__product__name = filters.CharFilter(field_name='purchase_order_detail__product__name'
                                                              , lookup_expr='icontains')

    class Meta:
        model = PurchaseReceiptDetail
        fields = ['purchase_receipt_code', 'purchase_order_detail__product__name']  # 设置搜索字段


class PurchaseReceiptDetailViewSet(BaseViewSet):
    queryset = PurchaseReceiptDetail.objects.all()
    serializer_class = PurchaseReceiptDetailSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_class = PurchaseReceiptFilter
    ordering_fields = ['id', 'purchase_receipt__code']  # 设置排序字段


class PurchaseReturnViewSet(BaseViewSet):
    queryset = PurchaseReturn.objects.all()
    serializer_class = PurchaseReturnSerializer
    search_fields = ['vendor__name', 'purchase_return_code']  # 设置搜索字段
    ordering_fields = ['id', 'code', 'status']  # 设置排序字段


class PurchaseReturnFilter(FilterSet):
    purchase_order_detail__product__name = filters.CharFilter(field_name='purchase_order_detail__product__name'
                                                              , lookup_expr='icontains')

    class Meta:
        model = PurchaseReturnDetail
        fields = ['purchase_return_code', 'purchase_order_detail__product__name']  # 设置搜索字段


class PurchaseReturnDetailViewSet(BaseViewSet):
    queryset = PurchaseReturnDetail.objects.all()
    serializer_class = PurchaseReturnDetailSerializer
    # 可以添加一些特定的设置，例如权限控制、分页等
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_class = PurchaseReturnFilter  # 设置搜索字段
    ordering_fields = ['id', 'purchase_return_code']  # 设置排序字段
